package com.springclouddemo.common.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.List;

public class Utils {
    public static int findIndex(String[] arr,String text) {
        int index=-1;
        for (int i = 0;i<arr.length;i++){
            if (arr[i].equalsIgnoreCase(text)){
                index = i;
                break;
            }
        }
        return  index;
    }
    public static int findIndex(List<String> list, String text) {
        int index=-1;
        for (int i = 0;i<list.size();i++){
            if (list.get(i).equalsIgnoreCase(text)){
                index = i;
                break;
            }
        }
        return  index;
    }
    /**
     * 通过枚举描述得到枚举类型
     *
     * @param c    枚举类型
     * @param text 枚举描述
     */
    public static <T extends Enum<T>> T getEnumFromText(Class<T> c,String text){
        if(c!=null && text!=null){
            try{
                return Enum.valueOf(c,text.trim().toUpperCase());
            }catch (IllegalArgumentException ex){
            }
        }
        return null;
    }

    /**
     * 动态设置字段值
     * @param obj 对象
     * @param fieldName 字段
     * @param value 值
     * @return
     */
    public static Object setValue(Object obj, String fieldName, Object value) {
        try {
            Field f = getField(obj.getClass(),fieldName);
            if(f==null){
                return null;
            }
            f.setAccessible(true);
            f.set(obj, value);
        } catch (Exception e) {
            return null;
        }
        return obj;
    }
    public static Field getField(Class c,String fieldName){
        Field[] fields=c.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            if(fields[i].getName().equalsIgnoreCase(fieldName))
            {
                return fields[i];
            }
        }
        return null;
    }

    /**
     * 对象克隆(深拷贝)
     * @param source 源对象
     * @param dest 目标对象
     * @throws Exception
     */
    public static void Clone(Object source, Object dest) throws Exception {
        BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(),Object.class);
        PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();
        BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(),Object.class);
        PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();
        try {
            for (int i = 0; i < sourceProperty.length; i++) {
                for (int j = 0; j < destProperty.length; j++) {
                    if (sourceProperty[i].getName().equals(destProperty[j].getName())  && sourceProperty[i].getPropertyType() == destProperty[j].getPropertyType()) {
                        destProperty[j].getWriteMethod().invoke(dest,sourceProperty[i].getReadMethod().invoke(source));
                        break;
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception("clone ERR:" + e.getMessage());
        }
    }
}
